<template>
  <view class="content" v-if="!loading">
    <view class="time-info">
      <view>
        <view class="section">
          <view class="info-title">事项</view>
          <view class="info-content" style="white-space: wrap;">{{issue}}</view>
        </view>
      </view>
      <view class="bottom-box">
        <view class="info-left">
          <view class="section">
            <view class="info-title">山向</view>
            <view class="info-content">{{panInfo.shan + "山" + panInfo.xiang + "向"}}  度数({{ panInfo.degreeRange }})</view>
          </view>
          <view class="section">
            <view class="info-title">干支</view>
            <view class="info-content">
              <view class="mod-text">
                <view class="sizhi-text">{{panInfo.yearGanZhi}}</view>
                <view class="sizhi-text">{{panInfo.hourGanZhi}}</view>
              </view>
            </view>
          </view>
        </view>
        <view class="info-right">
          <view class="section">
            <view class="info-title">年份</view>
            <view class="info-content">{{year}}</view>
          </view>
          <view class="section">
            <view class="info-title">选局</view>
            <view class="info-content">{{panInfo.yinOrYangDun}}遁{{panInfo.juShu}}局</view>
          </view>
        </view>
      </view>
      <view class="bottom-box">
        <view class="info-left">
          <view class="section">
            <view class="info-title">旬首</view>
            <view class="info-content">{{panInfo.xunShou}}</view>
          </view>
          <view class="section">
            <view class="info-title">空亡</view>
            <view class="info-content">{{panInfo.xunKong}}</view>
          </view>
        </view>
        <view class="info-center">
          <view class="section">
            <view class="info-title">值符</view>
            <view class="info-content">{{panInfo.zhiFu}}落{{panInfo.zhiFuIndex}}宫</view>
          </view>
          <view class="section">
            <view class="info-title">马星</view>
            <view class="info-content">{{panInfo.maXingContent}}</view>
          </view>
        </view>
        <view class="info-right">
          <view class="section">
            <view class="info-title">值使</view>
            <view class="info-content">{{panInfo.zhiShi}}门落{{panInfo.zhiShiIndex}}宫</view>
          </view>
        </view>
      </view>
    </view>
    <view class="content-box" ref="result">
      <!-- 卦象 -->
      <view class="table">
        <view class="td" :class="[current == rIndex ? 'selected-gong' : '']" v-for="(cell, rIndex) in panBody" :key="rIndex" @click="addInfo(cell, rIndex)">
          <template v-if="cell.index == 5"></template>
          <template v-else>
            <view class="flex-wrp-cell">
              <view class="flex-column">
                <view class="flex-item-cell">
                  <text>{{ cell.baShen }}</text>
                </view>
                <view class="flex-item-cell">
                  <template v-for="(txt, tIndex) in cell.tianPan">
                    <text :style="sihaiColor(txt, cell)">{{ txt }}</text>
                  </template>
                </view>
                <view class="flex-item-cell">
                  <template v-for="(txt, tIndex) in cell.diPan">
                    <text :style="sihaiColor(txt, cell)">{{ txt }}</text>
                  </template>
                </view>
              </view>
              <view class="flex-column">
                <view class="flex-item-cell">
                  <text></text>
                </view>
                <view class="flex-item-cell">
                  <text></text>
                </view>
                <view class="flex-item-cell">
                  <text></text>
                </view>
              </view>
              <view class="flex-column">
                <view class="flex-item-cell">
                  <text v-if="cell.xunKong == true">〇</text>
                </view>
                <view class="flex-item-cell">
                  <text>{{ cell.baXing }}</text>
                </view>
                <view class="flex-item-cell">
                  <text :style="sihaiColor(cell.newBaMen, cell)">{{ cell.newBaMen }}</text>
                </view>
              </view>
            </view>
          </template>
        </view>
      </view>
      <!-- 马星 -->
      <view class="maxing" :style="maxingPos(maxingItem)">
        <view>马</view>
      </view>
      <!-- 隐干 -->
      <view class="tmdh-top yingan-top">
        <view>{{panBody[1].yinGan}}</view>
      </view>
      <view class="tmdh-bottom yingan-bottom">
        <view>{{panBody[7].yinGan}}</view>
      </view>
      <view class="tmdh-left yingan-left">
        <view class="column-text">{{panBody[0].yinGan}}</view>
        <view class="column-text">{{panBody[3].yinGan}}</view>
        <view class="column-text">{{panBody[6].yinGan}}</view>
      </view>
      <view class="tmdh-right yingan-right">
        <view class="column-text">{{panBody[2].yinGan}}</view>
        <view class="column-text">{{panBody[5].yinGan}}</view>
        <view class="column-text">{{panBody[8].yinGan}}</view>
      </view>
    </view>
    <view class="tips">
      颜色说明：<span class="mu-color">入墓</span>、<span class="xing-color">击刑</span>、<span class="po-color">门迫</span>、<span class="xingmu-color">刑+墓</span>；
    </view>
    <view class="btn-box">
      <view class="tmdh-btn" @click="preview">放大查看</view>
      <view class="back-btn" @click="back">重新排盘</view>
    </view>
    <activity-box :positionId="1202"></activity-box>
    <uni-popup ref="popup" background-color="#fff" @change="change">
      <view class="popup-content">
        <view class="chengsheng-title">十二长生</view>
        <view class="popup-item">
          <view class="popup-title">天盘干：</view>
          <view class="popup-text">
            <view v-for="(item, index) in popInfo.changshengTianpanArr">
              <span>{{ item.name }}</span>
              <span>【</span>
              <span v-for="(e, i) in item.content">
                <span>{{ e }}</span>
                <span v-if="i!=item.content.length-1">、</span>
              </span>
              <span>】</span>
              <span v-if="index!=popInfo.changshengTianpanArr.length-1">，</span>
            </view>
          </view>
        </view>
        <view class="popup-item">
          <view class="popup-title">地盘干：</view>
          <view class="popup-text">
            <view v-for="(item, index) in popInfo.changshengDipanArr" :key="index">
              <span>{{ item.name }}</span>
              <span>【</span>
              <span v-for="(e, i) in item.content" :key="i">
                <span>{{ e }}</span>
                <span v-if="i!=item.content.length-1">、</span>
              </span>
              <span>】</span>
              <span v-if="index!=popInfo.changshengDipanArr.length-1">，</span>
            </view>
          </view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
  import {
    getQimenShanxiangApi
  } from '@/http/api/qimen/qimen.js'
  import {
    parseTime
  } from "@/utils/index.js"
  import html2canvas from 'html2canvas';
  export default {
    data() {
      return {
        loading: true,
        panInfo: {},
        panBody: {},
        tmdh: {},
        current: undefined,
        popInfo: {},
        isKe: 0
      }
    },
    mounted() {
      uni.showLoading({
        mask: true,
        title: "加载中",
      });
      let query = this.$route.query;
      let {
        degrees,
        year,
        issue,
      } = query;
      this.degrees = degrees;
      this.issue = issue;
      this.year = year;
      this.getQimenResult();
    },
    methods: {
      preview() {
        uni.showLoading({
          title: '加载中',
          mask: true
        });
        // loading框异步
        setTimeout(() => {
          html2canvas(this.$refs.result.$el,{
            scale: 2,
          }).then(canvas => { 
            let dataURL = canvas.toDataURL("image/png");
            uni.previewImage({
              urls: [dataURL]
            })
            uni.hideLoading()
          })
        }, 100)
      },
      back() {
        uni.navigateBack();
      },
      getQimenResult() {
        getQimenShanxiangApi({
          degrees: this.degrees,
          year: this.year,
          isKe: 2
        }).then(res => {
          if (res.code == 200) {
            this.panInfo = res.data.qiMenZao;
            this.tmdh = res.data.tianMenDiHuList;
            // 宫格按照指定顺序排序
            let arr = [4, 9, 2, 3, 5, 7, 8, 1, 6];
            this.panBody = [];
            for (let item of arr) {
              this.panBody.push(res.data.qimenGong.find(e => e.index == item));
            };
            // 判断马星位置
            this.maxingItem = this.panBody.find(e => e.baGua == res.data.qiMenZao.maXing);
            uni.hideLoading();
            this.loading = false;
          } else {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            });
          }
        })
      },
      maxingPos(item) {
        // 4, 2, 8, 6:左上,右上,左下, 右下
        if (item.index == 4) return "top:60rpx;left:80rpx;"
        if (item.index == 2) return "top:60rpx;right:80rpx;"
        if (item.index == 8) return "bottom:60rpx;left:80rpx;"
        if (item.index == 6) return "bottom:60rpx;right:80rpx;"
      },
      sihaiColor(zi, obj) {
        let item = obj.siHai.find(e => e.word == zi);
        if (item != undefined) {
          if (item.siHai == "迫") {
            return "color: #ff001a"
          }
          if (item.siHai == "墓") {
            let item = obj.siHai.find(e => e.word == zi && e.siHai == '刑');
            if (item != undefined) {
              return "color: #0098ff"
            }else{
              return "color: #ff6c00"
            }
          }
          if (item.siHai == "刑") {
            let item = obj.siHai.find(e => e.word == zi && e.siHai == '墓');
            if (item != undefined) {
              return "color: #0098ff"
            }else{
              return "color: #c800ff"
            }
          }
        }
        return ""
      },
      // 宫位信息
      addInfo(data, index) {
        if (index == 4) return
        if (this.current == index) {
          this.current = undefined;
          return
        };
        // 长生天盘内容展示
        let changshengTianpanArr = []
        for (let i=0;i<data.tianPan.length;i++) {
          let obj = [];
          // 循环步长
          let step = data.tianGanChangSheng.length/data.tianPan.length;
          let arr = [];
          for (let j=i*step;j<(i+1)*step;j++) {
            arr.push(data.tianGanChangSheng[j].title + '——' + data.tianGanChangSheng[j].content);
          };
          obj = {
            "name": data.tianPan[i],
            "content": arr
          };
          changshengTianpanArr.push(obj);
        }
        // 长生地盘内容展示
        let changshengDipanArr = []
        for (let i=0;i<data.diPan.length;i++) {
          let obj = [];
          // 循环步长
          let step = data.diZhiChangSheng.length/data.diPan.length;
          let arr = [];
          for (let j=i*step;j<(i+1)*step;j++) {
            arr.push(data.diZhiChangSheng[j].title + '——' + data.diZhiChangSheng[j].content);
          };
          obj = {
            "name": data.diPan[i],
            "content": arr
          };
          changshengDipanArr.push(obj);
        };
        data.changshengTianpanArr = changshengTianpanArr;
        data.changshengDipanArr = changshengDipanArr;
        this.popInfo = data;
        this.current = index;
        this.$refs.popup.open('bottom');
      },
      change(e) {
        if (e.show == false) this.current = undefined;
      }
    }
  }
</script>

<style>
  .content {
    box-sizing: border-box;
    height: 100%;
    width: 100%;
    overflow-y: scroll;
    color: #32312e;
    font-size: 28rpx;
    overflow-x: hidden;
  }

  .time-info {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    background-color: #c5a680;
    padding: 0 40rpx 20rpx;
    color: #ffffff;
    font-size: 24rpx;
    font-weight: bolder;
  }

  .bottom-box {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }

  .info-right {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
  }

  .info-left {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }

  .sizhi-title {
    display: flex;
    align-items: flex-end;
    white-space: nowrap;
    height: fit-content;
  }

  .mod-text {
    display: flex;
    flex-direction: row;
  }

  .sizhi-text {
    text-align: end;
    line-height: 24rpx;
    padding-right: 12rpx;
  }

  .more-time {
    font-size: 20rpx;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
  }

  .more-time-item {
    display: flex;
    flex-direction: row;
    align-items: center;
  }

  .section {
    display: flex;
    flex-direction: row;
    padding-bottom: 10rpx;
    align-items: center;
  }

  .info-title {
    white-space: nowrap;
    background-color: #4b240d;
    padding: 0 12rpx;
    
    letter-spacing: 4rpx;
    height: fit-content;
  }

  .info-content {
    white-space: nowrap;
    padding: 0 12rpx;
  }

  .content-box {
    padding: 100rpx;
    position: relative;
    width: 100%;
    box-sizing: border-box;
    border: 2rpx solid #ccc;
    
  }

  /* 宫格 */
  .table {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
  }

  .tr {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-bottom: 12rpx;
  }

  .td {
    box-sizing: border-box;
    width: 33%;
    border-radius: 30rpx;
    border: solid 2rpx #4b240d;
    box-shadow: 0 0 4px #666;
    background: #fff;
  }

  .lightGrey {
    border: none;
  }

  .flex-wrp-cell {
    display: flex;
    justify-content: space-around;
    height: 100%;
    width: 100%;
  }

  .flex-column {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    width: 33%;
  }

  .flex-item-cell {
    height: 33%;
    text-align: center;
    line-height: 68rpx;
    white-space: nowrap;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .maxing {
    position: absolute;
/*    bottom: 60rpx;
    right: 80rpx; */
    box-sizing: border-box;
    color: red;
  }
  
  .tmdh-top {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    position: absolute;
    top: 10rpx;
    left: 10rpx;
    padding: 0 80rpx;
    box-sizing: border-box;
  }
  
  .yingan-top {
    top: 50rpx
  }
  
  .tmdh-left {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    position: absolute;
    top: 10rpx;
    left: 10rpx;
    padding: 80rpx 0;
    box-sizing: border-box;
  }
  
  .yingan-left {
    left: 50rpx;
  }
  
  .tmdh-right {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    position: absolute;
    top: 10rpx;
    right: 10rpx;
    padding: 80rpx 0;
    box-sizing: border-box;
  }
  
  .yingan-right {
    right: 50rpx;
  }
  
  .tmdh-bottom {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    position: absolute;
    bottom: 10rpx;
    left: 10rpx;
    padding: 0 80rpx;
    box-sizing: border-box;
  }
  
  .yingan-bottom {
    bottom: 50rpx
  }
  
  .column-text {
    writing-mode: vertical-lr;
  }

  .btn-box {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-bottom: 30rpx;
    box-sizing: border-box;
  }

  .tmdh-btn {
    width: 521rpx;
    height: 78rpx;
    
    font-size: 32rpx;
    color: #fff;
    background-color: #c6a680;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    margin-bottom: 30rpx;
  }

  .back-btn {
    width: 521rpx;
    height: 78rpx;
    
    font-size: 32rpx;
    color: #c6a680;
    background-color: #fff;
    border: 2rpx solid #c6a680;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
  .tips {
    width: 100%;
    text-align: center;
    padding: 20rpx 0;
  }
  .mu-color {
    color: #ff6c00;
  }
  .xing-color {
    color: #c800ff;
  }
  .po-color {
    color: #ff001a;
  }
  .xingmu-color {
    color: #0098ff;
  }
  .selected-gong {
    background: #F2D4D2;
  }
  .popup-content {
    display: flex;
    flex-direction: column;
    height: 250rpx;
    padding: 30rpx 50rpx;
    font-size: 30rpx;
  }
  .chengsheng-title {
    font-size: 32rpx;
    font-weight: bold;
    margin-bottom: 20rpx;
  }
  .popup-item {
    display: flex;
    flex-direction: row;
    padding-bottom: 10rpx;
    margin-bottom: 10rpx;
    border-bottom: 2rpx solid #EDEDED;
  }
  .popup-title {
    color: red;
    white-space: nowrap;
  }
  .popup-text {
    
  }
</style>